// pages/article/article.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({
  data: {
    currentTab: 0, //预设当前项的值
    scrollLeft: 20, //tab标题的滚动条位置
    head_info: '',//所有头条信息
    page: 1,//页数
    limit: 6,//每页条数
    tid: 0,//选中分类id
    prodis: 'none',//底部提示是否显示
    showView: false //发表弹窗
  },


  // 点击收藏
  collectTop: function () {
    wx.showToast({
      title: '收藏成功',
    })
  },

  // 点击查看更多评论
  clickmore:function(){
    // console.log(123)
    wx.showToast({
      title: '查看更多',
    })
  },

  // 点击回复按钮
  reply:function(){
    // wx.showToast({
    //   title: '回复信息',
    // })
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  // 点击发表
  clickfabiao:function(){
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
    wx.showToast({
      title: '发表成功',
    })
  },
  // 点赞
  prise:function(){
   wx.showToast({
     title: '点赞成功',
   })
  },

  // 分类滑块
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
      tid: cur,
    }
    ikz.request(path, data, false, (res) => {
      
      if (res.data.status == 'success') {
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          tid: cur,
          page: page,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });

    // 设置要显示的数据

    var index = e.target.dataset.index;
    var info = [];
    for (var i = 0; i < that.data.storageinfo.length; i++) {

      if (cur == that.data.storageinfo[i].type_id) {
        info.push(that.data.storageinfo[i])
      } else if (cur == 0) {
        info = that.data.storageinfo;
      }
    }

    that.setData({
      currentinfo: info,
      index: index
    })
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur
      })
    }
    that.checkCor();

  },
  //   判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.index >= 4) {
      this.setData({
        scrollLeft: this.data.index * 100
      })
    } else if (this.data.index < 4) {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  onLoad: function (options) {
    ikz.over(this);
    showView: (options.showView == "true" ? true : false)
    // 页面初始化 options为页面跳转所带来的参数
    // 页面显示
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var tid = 0;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    ikz.request(path, data, false, (res) => {
      if (res.data.status == 'success') {
        var typeid = [];
        for (var i = 0; i < res.data.head_type_info.length; i++) {
          typeid[i] = res.data.head_type_info[i].id;
        }
      
        res.data.head_type_info.unshift({ head_type_id: 0, head_type_name: '全部' });//给其分类前面添加一个 全部
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          storageinfo: res.data.head_type_info, //存储数据
          currentinfo: res.data.head_type_info, //显示数据
          types: res.data.head_type_info,//全部分类
          type_id: typeid,
          tid: tid,
          page: page,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  // 详情页
  toContent: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../content/content?' + 'hid=' + hid,
    })
  },

  // 详情页
  toPlay: function (e) {
    var that = this;
    var hid = e.currentTarget.dataset.hid;
    wx.navigateTo({
      url: '../content/content?' + 'hid=' + hid,
    })
    var vid = e.currentTarget.dataset.vid;
    this.videoContext = wx.createVideoContext(vid);
    this.videoContext.pause();
  },
  
  toTop:function(){
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  onReady: function () {
    // 页面渲染完成
    //this.videoContext = wx.createVideoContext('myVideo17');
  },
  // inputValue: '',
  // bindInputBlur: function (e) {
  //   this.inputValue = e.detail.value;
  // },
  // bindSendDanmu: function () {
  //   this.videoContext.sendDanmu({
  //     text: this.inputValue,
  //     color: this.getRandomColor()
  //   })
  // },
  
  //数字转换
  changeNum(num){
    if (num > 0 && num < 10000) {
      num = num / 100;
      num = Math.ceil(num) / 10;
      var newnum = num + 'k';
    } else if (num >= 10000) {
      num = num / 1000;
      num = Math.ceil(num) / 10;
      var newnum = num + 'w';
    } else {
      var newnum = 0;
    }
    return newnum;
  },

  onShow: function () {
    
    
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中...',
    });
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Index/index';
    var tid = that.data.tid;
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    ikz.request(path, data, false, (res) => {
      if (res.data.status == 'success') {
        var typeid = [];
        for (var i = 0; i < res.data.head_type_info.length; i++) {
          typeid[i] = res.data.head_type_info[i].id;
        }

        res.data.head_type_info.unshift({ head_type_id: 0, head_type_name: '全部' });//给其分类前面添加一个 全部
        that.setData({
          head_info: res.data.head_info,//所有头条信息
          storageinfo: res.data.head_type_info, //存储数据
          currentinfo: res.data.head_type_info, //显示数据
          types: res.data.head_type_info,//全部分类
          type_id: typeid,
          tid: tid,
          page: page,
        });
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })  
    var that = this;
    var page = that.data.page + 1;
    var limit = that.data.limit;
    var tid = that.data.tid;
    var old_info = that.data.head_info;
    that.setData({
      page: page,
    })
    var path = 'Index/index';
    var data = {
      page: page,
      limit: limit,
      tid: tid,
    }
    console.log(data);
    console.log(6767);
    ikz.request(path, data, false, (res) => {

      if (res.data.status == 'success') {
        var new_info = res.data.head_info;
        if (new_info.length) {
          var all_info = old_info.concat(new_info);
          var prodis = 'none';
        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          head_info: all_info,//所有头条信息
          tid: tid,
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },
  getRandomColor() {
    let rgb = [];
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16);
      color = color.length == 1 ? '0' + color : color;
      rgb.push(color);
    }
    return '#' + rgb.join('');
  },
})
